package com.wondersgroup.fjzlpt.yyzyzx.service.impl.yyzygl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.CwszxxDao;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.CwyyfsyxjDao;
import com.wondersgroup.fjzlpt.yyzyzx.service.inter.yyzygl.CwszService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class CwszServiceImpl implements CwszService {

	@Autowired
	private CwszxxDao cwszxxDao;
	
	@Autowired
	private CwyyfsyxjDao cwyyfsyxjDao;

	@Override
	public JSONObject save(Map map) {
		JSONObject result = new JSONObject();
		//ZT设置
		String SFXYSH = map.get("SFXYSH").toString();
		if("1".equals(SFXYSH)){
			map.put("ZT", "2");
		}else{
			map.put("ZT", "1");
		}
		
		String type = map.get("type").toString();
		if("add".equals(type)){
			insert(map, result);
		}else{
			update(map, result);
		}
		return result;
	}
	
	/**
	 * 添加床位设置
	 * @param map
	 * @param result
	 */
	private void insert(Map map, JSONObject result){
		try {
			String CWSZBH = UUID.randomUUID().toString();
			map.put("CWSZBH", CWSZBH);
			Date nowDate = new Date();
			map.put("XZSJ", nowDate);
			map.put("GXSJ", nowDate);
			
			//插入床位设置信息
			cwszxxDao.insert(map);
			//插入床位预约方式优先级
			insertYyfsyxj(map);
			
			result.put("state", "1");
			result.put("info", "床位设置信息保存成功！");
		} catch (Exception e) {
			result.put("info", "床位设置信息保存失败！");
			throw new RuntimeException("床位设置信息保存失败！");
		}
	}
	
	/**
	 * 修改床位设置
	 * @param map
	 * @param result
	 */
	private void update(Map map, JSONObject result){
		try {
			map.remove("XZYH");
			
			Date nowDate = new Date();
			map.put("GXSJ", nowDate);
			
			//更新床位设置信息
			cwszxxDao.updateForPrimaryKey(map);
			//插入床位预约方式优先级
			insertYyfsyxj(map);
			
			result.put("state", "1");
			result.put("info", "床位设置信息修改成功！");
		} catch (Exception e) {
			result.put("info", "床位设置信息修改失败！");
			throw new RuntimeException("床位设置信息修改失败！");
		}
	}
	
	/**
	 * 批量插入床位预约方式优先级
	 * @param map
	 */
	private void insertYyfsyxj(Map map){
		Object yyyxj = map.get("yyyxj");
		if(yyyxj!=null && !"".equals(yyyxj)){
			//删除床位预约方式优先级
			cwyyfsyxjDao.deleteByCwszbm(map.get("CWSZBH").toString());
			
			JSONArray yyyxjArr = JSONArray.parseArray(yyyxj.toString());
			if(yyyxjArr != null && yyyxjArr.size()>0){
				List list = new ArrayList();
				for(int i=0;i<yyyxjArr.size();i++){
					Map yyfsyxjMap = (Map)yyyxjArr.getJSONObject(i);
					yyfsyxjMap.put("CWSZBH", map.get("CWSZBH"));
					list.add(yyfsyxjMap);
				}
				cwyyfsyxjDao.insertBatch(list);
			}
		}
	}

	/**
	 * 分页查询床位设置信息
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryPage(Map map) {
		return cwszxxDao.queryPage(map);
	}

	/**
	 * 删除床位设置信息
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject delete(Map map) {
		JSONObject result = new JSONObject();
		try {
			cwszxxDao.deleteForPrimaryKey(map);
			cwyyfsyxjDao.deleteByCwszbm(map.get("CWSZBH").toString());
			result.put("state", "1");
			result.put("info", "删除成功！");
		} catch (Exception e) {
			result.put("info", "床位设置信息删除失败！");
			throw new RuntimeException("床位设置信息删除失败！");
		}
		return result;
	}

	/**
	 * 查询床位预约优先级
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryCwyyyxj(Map map) {
		JSONObject result = new JSONObject();
		try {
			JSONArray yxjArray = cwyyfsyxjDao.queryByCwszbh(map.get("CWSZBH").toString());
			result.put("data", yxjArray);
			result.put("state", "1");
		} catch (Exception e) {
			result.put("info", "床位预约优先级查询失败，请稍后再试!");
			throw new RuntimeException("床位预约优先级查询失败，请稍后再试!");
		}
		return result;
	}
	
}
